package com.kongge.formlikedemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.kongge.formlikedemo.R;

/**
 * author:kongge
 * date:2018/7/23
 * layout:
 * description:
 */

public class DIYView extends View {

    private String text;

    private Paint paint;

    private int textSize = 20;

    public DIYView(Context context) {
        super(context);
        init(context, null);
    }

    public DIYView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DIYView);
            text = a.getString(R.styleable.DIYView_diyViewText);
            a.recycle();
        }
        paint = new TextPaint();
        paint.setTextSize(textSize);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (text != null) {
            canvas.drawText(text, 0, (getHeight() + textSize) / 2, paint);
        }
    }
}
